import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/24 20:59
 * @description 数学
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {2,2,2,1,2,2,1,2,2,2};
        solution.numberOfSubarrays(nums, 2);
    }
    public int numberOfSubarrays(int[] nums, int k) {
        int n = nums.length;
        // 记录奇数的下标，那么对于第i个奇数，odd[i-1], odd[i]之间全部为偶数，odd[i+k-1], odd[i + k]间的数也是偶数
        int[] odd = new int[n + 2];
        int ans = 0, cnt = 0;
        for (int i = 0; i < n; ++i) {
            if ((nums[i] & 1) != 0) {
                odd[++cnt] = i;
            }
        }
        // 为了更好的处理边界问题
        odd[0] = -1;
        odd[++cnt] = n;
        for (int i = 1; i + k <= cnt; ++i) {
            ans += (odd[i] - odd[i - 1]) * (odd[i + k] - odd[i + k - 1]);
        }
        return ans;

    }
}
